package cn.kust.servicesocial.controller;

import cn.kust.microtourismcommon.constant.CommonStatusEnum;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.servicesocial.service.IFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WY
 * @data 2022/7/21 20:33
 **/
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    IFollowService iFollowService;

    // 取消关注
    @RequestMapping(value = "/delete/{userid}/{friendid}", method = RequestMethod.DELETE)
    public ResponseResult deleteFriend(@PathVariable("userid") String userid, @PathVariable("friendid") String friendid) {
        //这里已经验证过了id，id保证合法
        System.out.println(userid);
        System.out.println(friendid);
        Long userId = Long.parseLong(userid);
        Long friendId = Long.parseLong(friendid);
        return iFollowService.deleteFollowById(userId, friendId);

    }

    // 添加关注
    @RequestMapping(value = "/add/{userid}/{friendid}", method = RequestMethod.PUT)
    public ResponseResult addFriend(@PathVariable("userid") String userid, @PathVariable("friendid") String friendid) {
        Long userId = Long.parseLong(userid);
        Long friendId = Long.parseLong(friendid);

        return iFollowService.addFollow(userId, friendId);

    }

    //根据用户id查询用户的关注列表
    @RequestMapping(value = "/get-follow-list/{userid}", method = RequestMethod.GET)
    public ResponseResult getFollowList(@PathVariable("userid") String userid){
        Long userId = Long.parseLong(userid);
        return iFollowService.findFollowById(userId);
    }

    //根据用户id查询用户的粉丝列表
    @RequestMapping(value = "/get-fans-list/{userid}", method = RequestMethod.GET)
    public ResponseResult getFansList(@PathVariable("userid") String userid){
        Long userId = Long.parseLong(userid);
        return iFollowService.findFansById(userId);
    }

    //根据两个用户id查询关注状态
    @RequestMapping(value = "/state/{userId}/{friendId}", method = RequestMethod.GET)
    public ResponseResult getRelation(@PathVariable("userId") String userId, @PathVariable("friendId") String friendId){
        Long userid = Long.parseLong(userId);
        Long friendid = Long.parseLong(friendId);
        if (userid.equals(friendid)){
            return ResponseResult.fail(CommonStatusEnum.FOLLOW_INDEX_ERROR.getCode(),CommonStatusEnum.FOLLOW_INDEX_ERROR.getValue());
        }
        return iFollowService.followRelation(userid,friendid);
    }


}
